package com.xbb.controller.user;

import com.xbb.common.RespResult;
import com.xbb.constant.SessionConstant;
import com.xbb.entity.Questionnaire;
import com.xbb.entity.User;
import com.xbb.entity.UserQuestionnaire;
import com.xbb.mapper.QuestionnaireMapper;
import com.xbb.mapper.UserMapper;
import com.xbb.mapper.UserQuestionnaireMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/userQuestionnaire")
public class UserQuestionnaireController {

    private String prefix = "/user/questionnaire/";

    @Resource
    private UserQuestionnaireMapper userQuestionnaireMapper;
    @Resource
    private QuestionnaireMapper questionnaireMapper;

    // 问卷调查管理
    @RequestMapping("/manage.html")
    public String manageHtml () {
        return prefix + "user";
    }

    // 分页数据
    @ResponseBody
    @RequestMapping("/data")
    public RespResult data (@RequestParam(value = "title", required = false, defaultValue = "") String title,
                            @RequestParam(value = "name", required = false, defaultValue = "") String name,
                            @RequestParam(value = "cellphone", required = false, defaultValue = "") String cellphone,
                            @RequestParam(value = "questionnaireId", required = false) Integer questionnaireId,
                            HttpSession session) {
        User user = (User) session.getAttribute(SessionConstant.KEY_USER);
        if (title.equals("")) title = null;
        if (name.equals("")) name = null;
        if (cellphone.equals("")) cellphone = null;
        List<UserQuestionnaire> data = userQuestionnaireMapper.selectListByPaging(
                title,
                name,
                cellphone,
                questionnaireId,
                user.getId()
        );
        int count = userQuestionnaireMapper.selectCountByPaging(
                title,
                name,
                cellphone,
                questionnaireId,
                user.getId()
        );

        Set<UserQuestionnaire> userQuestionnaireSet = new HashSet<>();
        for (UserQuestionnaire userQuestionnaire : data) {
            userQuestionnaireSet.add(userQuestionnaire);
        }

        RespResult respResult = new RespResult();
        respResult.success(userQuestionnaireSet, count);
        return respResult;
    }

    // 添加
    @ResponseBody
    @RequestMapping("/add")
    public RespResult add (@RequestBody List<UserQuestionnaire> userQuestionnaireList, HttpSession session) {
        User user = (User) session.getAttribute(SessionConstant.KEY_USER);
        for (UserQuestionnaire userQuestionnaire : userQuestionnaireList) {
            userQuestionnaire.setUserId(user.getId());
            userQuestionnaireMapper.insertSelective(userQuestionnaire);
        }
        // 问卷参与人数 + 1
        questionnaireMapper.updateAddCountById(userQuestionnaireList.get(0).getQuestionnaireId());
        return new RespResult();
    }

    // 调查结果详情
    @RequestMapping("/result.html")
    public String resultHtml (@RequestParam("id") int id, Model model) {
        UserQuestionnaire userQuestionnaire = userQuestionnaireMapper.selectByPrimaryKey(id);
        Questionnaire questionnaire = questionnaireMapper.selectByPrimaryKey(userQuestionnaire.getQuestionnaireId());
        List<UserQuestionnaire> userQuestionnaireList = userQuestionnaireMapper.selctListByQuestionnaireId(
                userQuestionnaire.getQuestionnaireId(), userQuestionnaire.getUserId()
        ) ;
        model.addAttribute("questionnaire", questionnaire);
        model.addAttribute("userQuestionnaireList", userQuestionnaireList);
        return prefix + "result";
    }

}
